package com.xckj.cycling.fragment

import android.Manifest
import android.os.Bundle
import android.view.View
import com.xckj.cycling.framework.lazyGlobalViewModel
import com.tbruyelle.rxpermissions3.Permission
import com.tbruyelle.rxpermissions3.RxPermissions
import com.xckj.cycling.framework.BaseFragmentKt
import com.xckj.cycling.utils.MapUtils
import com.xckj.cycling.view.RouteMapView
import com.xckj.cycling.viewmodel.MapViewModel
import io.reactivex.rxjava3.core.Observer
import io.reactivex.rxjava3.disposables.Disposable

abstract class BaseMapFragment : BaseFragmentKt() {

    protected val viewModel: MapViewModel by lazyGlobalViewModel()
    abstract val mapView: RouteMapView

    private val mapViewUtils by lazy { MapUtils(mapView) }


    override fun initView(root: View, savedInstanceState: Bundle?) {
        super.initView(root, savedInstanceState)
        viewLifecycleOwner.lifecycle.addObserver(mapView)
        RxPermissions(this).requestEachCombined(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        ).subscribe(object :
            Observer<Permission> {
            override fun onSubscribe(d: Disposable) {
            }

            override fun onError(e: Throwable) {
            }

            override fun onComplete() {
            }

            override fun onNext(t: Permission) {
                if (t.granted) {
                    onMapReady()
                } else {
                }
            }

        })
    }


    private fun onMapReady() {
//        mapView.getMapboxMap().addOnMapClickListener(object : OnMapClickListener {
//            override fun onMapClick(point: Point): Boolean {
//                mapViewUtils.addAnnotationToMap(requireContext(), point)
//                mapView.requestRoute(point)
//                return true
//            }
//
//        })
    }


}